//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "StrippedOptionlet.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/StrippedOptionletBase.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::IVector<DateTime>^ optionletDates, Cephei::IVector<Double>^ strikes, Cephei::IMatrix<Cephei::QL::IQuote^>^ prm1, Cephei::QL::Times::IDayCounter^ dc) : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
    CCalendar^ _Ccalendar;
    CIborIndex^ _CiborIndex;
    CoVector<DateTime>^ _CoptionletDates;
    CoVector<Double>^ _Cstrikes;
    CoMatrix<Cephei::QL::IQuote^>^ _Cprm1;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phStrippedOptionlet = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _CiborIndex = safe_cast<CIborIndex^> (iborIndex);
        _CiborIndex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _iborIndex = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_CiborIndex->GetShared ()); 
        CoVector<DateTime>^ _CoptionletDates = safe_cast<CoVector<DateTime>^> (optionletDates);
        _CoptionletDates->Lock();
        INativeVector<DateTime>^ _NCIoptionletDates = _CoptionletDates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCoptionletDates = safe_cast<CDateTimeVector^>(_NCIoptionletDates);
        std::vector<QuantLib::Date>& _optionletDates = static_cast<std::vector<QuantLib::Date>&> (_NCoptionletDates->GetReference ());
        CoVector<Double>^ _Cstrikes = safe_cast<CoVector<Double>^> (strikes);
        _Cstrikes->Lock();
        INativeVector<Double>^ _NCIstrikes = _Cstrikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Rate>& _strikes = static_cast<std::vector<QuantLib::Rate>&> (_NCstrikes->GetReference ());
        _Cprm1 = safe_cast<CoMatrix<Cephei::QL::IQuote^>^> (prm1);
        _Cprm1 ->Lock ();
        INativeMatrix<Cephei::QL::IQuote^>^ _NCIprm1 = _Cprm1->getFeature (NativeFeature::Handle);
        CQuoteMatrix^ _NCprm1 = safe_cast<CQuoteMatrix^>(_NCIprm1);
        std::vector<std::vector<Handle<QuantLib::Quote> > >& _prm1 = static_cast<std::vector<std::vector<Handle<QuantLib::Quote> > >&> (_NCprm1->GetHandle ());
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppStrippedOptionlet = new boost::shared_ptr<QuantLib::StrippedOptionlet> (new QuantLib::StrippedOptionlet ( _settlementDays,  _calendar,  _bdc,  _iborIndex,  _optionletDates,  _strikes,  _prm1,  _dc ));
        SetStrippedOptionletBase (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
        if (_CiborIndex != nullptr) _CiborIndex->Unlock();
        if (_CoptionletDates != nullptr) _CoptionletDates->Unlock();
        if (_Cstrikes != nullptr) _Cstrikes->Unlock();
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (boost::shared_ptr<QuantLib::StrippedOptionlet>& childNative, Object^ owner) : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
#ifdef HANDLE
	_phStrippedOptionlet = NULL;
#endif
	_ppStrippedOptionlet = &childNative;
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (QuantLib::StrippedOptionlet& childNative, Object^ owner) : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
#ifdef HANDLE
	_phStrippedOptionlet = NULL;
#endif
	_ppStrippedOptionlet = new boost::shared_ptr<QuantLib::StrippedOptionlet> (&childNative);
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
    _StrippedOptionletOwner = owner;
    _StrippedOptionletBaseOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (CStrippedOptionlet^ copy) : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
#ifdef HANDLE
	_phStrippedOptionlet = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppStrippedOptionlet = new boost::shared_ptr<QuantLib::StrippedOptionlet> (copy->GetShared());
        _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (System::Type^ t) : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
#ifdef HANDLE
	_phStrippedOptionlet = NULL;
#endif
	if (!t->IsSubclassOf(CStrippedOptionlet::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (QuantLib::Handle<QuantLib::StrippedOptionlet>& childNative, Object^ owner)  : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
	_phStrippedOptionlet = &childNative;
	_ppStrippedOptionlet = &static_cast<boost::shared_ptr<QuantLib::StrippedOptionlet>>(childNative.currentLink());
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
    _StrippedOptionletOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (QuantLib::Handle<QuantLib::StrippedOptionlet> childNative)  : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
	_phStrippedOptionlet = &childNative;
	_ppStrippedOptionlet = &static_cast<boost::shared_ptr<QuantLib::StrippedOptionlet>>(childNative.currentLink());
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::CStrippedOptionlet (QuantLib::StrippedOptionlet childNative)  : CStrippedOptionletBase(CStrippedOptionlet::typeid)
{
#ifdef HANDLE
	_phStrippedOptionlet = NULL;
#endif
	_ppStrippedOptionlet = new boost::shared_ptr<QuantLib::StrippedOptionlet> (new QuantLib::StrippedOptionlet (childNative));
    _ppStrippedOptionletBase = new boost::shared_ptr<QuantLib::StrippedOptionletBase> (boost::dynamic_pointer_cast<QuantLib::StrippedOptionletBase> (*_ppStrippedOptionlet));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::~CStrippedOptionlet ()
{
    if (_ppStrippedOptionlet != NULL)
    {
	    delete _ppStrippedOptionlet;
        _ppStrippedOptionlet = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::!CStrippedOptionlet ()
{
    if (_ppStrippedOptionlet != NULL)
    {
	    delete _ppStrippedOptionlet;
    }
}
QuantLib::StrippedOptionlet& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::GetReference ()
{
    if (_ppStrippedOptionlet == NULL) throw gcnew NativeNullException ();
	return **_ppStrippedOptionlet;
}
boost::shared_ptr<QuantLib::StrippedOptionlet>& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::GetShared ()
{
    if (_ppStrippedOptionlet == NULL) throw gcnew NativeNullException ();
	return *_ppStrippedOptionlet;
}
QuantLib::StrippedOptionlet* Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::GetPointer ()
{
    if (_ppStrippedOptionlet == NULL) throw gcnew NativeNullException ();
	return &**_ppStrippedOptionlet;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::StrippedOptionlet>& Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::GetHandle ()
{
	if (_phStrippedOptionlet == NULL)
	{
		_phStrippedOptionlet = new Handle<QuantLib::StrippedOptionlet> (*_ppStrippedOptionlet);
	}
	return *_phStrippedOptionlet;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::HasNative () 
{
	return (_ppStrippedOptionlet != NULL);
}

Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::AtmOptionletRates::get ()
{
    try
    {
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppStrippedOptionlet)->atmOptionletRates ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::BusinessDayConventionEnum Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppStrippedOptionlet)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppStrippedOptionlet)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppStrippedOptionlet)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::OptionletFixingDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppStrippedOptionlet)->optionletFixingDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::OptionletFixingTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppStrippedOptionlet)->optionletFixingTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::OptionletMaturities::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppStrippedOptionlet)->optionletMaturities ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::OptionletStrikes (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	std::vector<QuantLib::Rate>& _rv = (std::vector<QuantLib::Rate>&)(*_ppStrippedOptionlet)->optionletStrikes ( _i );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::OptionletVolatilities (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
    	std::vector<QuantLib::Volatility>& _rv = (std::vector<QuantLib::Volatility>&)(*_ppStrippedOptionlet)->optionletVolatilities ( _i );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppStrippedOptionlet)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::IStrippedOptionlet^ Cephei::QL::Termstructures::Volatility::Optionlet::CStrippedOptionlet_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ calendar, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::IVector<DateTime>^ optionletDates, Cephei::IVector<Double>^ strikes, Cephei::IMatrix<Cephei::QL::IQuote^>^ prm1, Cephei::QL::Times::IDayCounter^ dc)
{
    return gcnew CStrippedOptionlet ( settlementDays,  calendar,  bdc,  iborIndex,  optionletDates,  strikes,  prm1,  dc);
}
